﻿/*
 * @author: wizardc
 */

#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace Dou.Controller
{
    /// <summary>
    /// 限制机位的摄像机调试器
    /// </summary>
    [RequireComponent(typeof(RestrictCameraController))]
    public class RestrictCameraDebugger : MonoBehaviour
    {
        private static readonly Color LimitColor = Color.yellow;
        
        private RestrictCameraController _controller;
        
        void Awake()
        {
            _controller = GetComponent<RestrictCameraController>();
        }

        void OnDrawGizmos()
        {
            if (!EditorApplication.isPlaying)
            {
                return;
            }
            
            var cubicLimitPos = _controller.cubicLimitPos;
            var trapezoidLimitPos = _controller.trapezoidLimitPos;

            if (cubicLimitPos[0] == Vector3.zero && cubicLimitPos[1] == Vector3.zero)
            {
                // 立方梯形
                var underTopLeft = trapezoidLimitPos[0];
                var underTopRight = trapezoidLimitPos[1];
                var underBottomLeft = trapezoidLimitPos[2];
                var underBottomRight = trapezoidLimitPos[3];
                var aboveTopLeft = trapezoidLimitPos[4];
                var aboveTopRight = trapezoidLimitPos[5];
                var aboveBottomLeft = trapezoidLimitPos[6];
                var aboveBottomRight = trapezoidLimitPos[7];
                
                Gizmos.color = LimitColor;
                
                Gizmos.DrawLine(underTopLeft, underTopRight);
                Gizmos.DrawLine(underTopRight, underBottomRight);
                Gizmos.DrawLine(underBottomRight, underBottomLeft);
                Gizmos.DrawLine(underBottomLeft, underTopLeft);
                
                Gizmos.DrawLine(aboveTopLeft, aboveTopRight);
                Gizmos.DrawLine(aboveTopRight, aboveBottomRight);
                Gizmos.DrawLine(aboveBottomRight, aboveBottomLeft);
                Gizmos.DrawLine(aboveBottomLeft, aboveTopLeft);
                
                Gizmos.DrawLine(underTopLeft, aboveTopLeft);
                Gizmos.DrawLine(underTopRight, aboveTopRight);
                Gizmos.DrawLine(underBottomRight, aboveBottomRight);
                Gizmos.DrawLine(underBottomLeft, aboveBottomLeft);
            }
            else
            {
                // 立方体
                var min = cubicLimitPos[0];
                var max = cubicLimitPos[1];
                
                Gizmos.color = LimitColor;
                Gizmos.DrawWireCube((min + max) / 2f, max - min);
            }
        }
    }
}

#endif